package yxy.game.pm2.command.camptower;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.BattleMessage;
import yxy.game.pm2.message.CampTowerMessage;
import yxy.game.pm2.module.battle.BattleType;
import yxy.game.pm2.module.camptower.CampTowerHandle;

/**
 * 阵营塔挑战执行请求
 *
 * @ClassName CMDCampTowerChanllengeExecute
 * @Author jingtao-zheng
 * @Date 2022/8/2 10:37
 * @Version 1.0
 **/
@MessageType(proto = Command.CampTowerChanllengeExecute, classtype = CampTowerMessage.CampTowerChanllengeExecuteRequest.class)
public class CMD_CampTowerChanllengeExecute extends Command<CampTowerMessage.CampTowerChanllengeExecuteRequest> {
    @Override
    public void execute(MessageSource messageSource, CampTowerMessage.CampTowerChanllengeExecuteRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        BattleMessage.BattleExecuteRequest battleReq = request.getBattleReq();
        CampTowerMessage.CampTowerChanllengeExecuteResponse.Builder response = CampTowerMessage.CampTowerChanllengeExecuteResponse.newBuilder();
        //获取阵营塔类型
        int type = request.getType().getNumber();
        //活动是否开启
        boolean finish = CampTowerHandle.isFinish(type);
        if(!finish){
            send(messageSource, response, Result.ActNoOpen);
        }
        //之前战斗是否结束
        BattleType battleType = CampTowerHandle.matchBattleType(type);
        //一定带战斗类型
        CampTowerHandle handle = new CampTowerHandle(battleType);
        if (handle.isBattling(playerCode)) {
            send(messageSource, response, Result.BattleIsRunning);
            return;
        }
        try {
            //战斗录像
            response.setBattleResp(handle.battleResponseExecute(playerCode, battleReq,type));
            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, CampTowerMessage.CampTowerChanllengeExecuteResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
